<?php
/**
 * Fang_Controller_Photo
 * 
 * 作者:  姚刚 (yaogang@the9sense.com)
 * 创建时间: 2011-03-16 06:23:52
 * 修改记录: 
 * 
 * $Id: Photo.php 18 2011-04-17 12:50:07Z coosuu $
 */

class Fang_Controller_Photo extends Fang_Controller_Base {

    /**
     * newsAction 
     * 网站最新消息(图片幻灯片和文章列表)
     * 
     * @return void
     */
    public function newsAction() {

        try {

            $intro = $this->intro;
            $cid = $intro['i_news_cid'];
            if (!$cid) {
                return $this->errorMessage('没有设置最新文章类型ID');
            }

            $pageOptions = array('perPage' => 5,
                                 'currentPage' => 1);
            $articleService =& Service::factory('Article');
            $articles = $articleService->listByCid($cid, $pageOptions);
        } catch (Exception $e) {
            return $this->errorMessage($e->getMessage());
        }

        $pids = $aids = $titles = array();
        foreach ($articles as $a) {
            if ($a['a_cover_id']) {
                $pids[] = $a['a_cover_id'];
                $aids[] = $a['aid'];
                $titles[] = $a['a_title'];
            }
        }

        if ($pids) {
            try {
                $photoService =& Service::factory('Photo');
                $photos = $photoService->listByPids($pids);
            } catch (Exception $e) {
                return $this->_errorMessage($e->getMessage());
            }

            $imgs = array();
            foreach ($photos as $p) {
                $imgs[] = '/photos/' . $p['p_src'];
            }
        }

        $this->_listCates();
        $this->view->set('articles', $articles);
        $this->view->set('aids', json_encode($aids));
        $this->view->set('photos', json_encode($imgs));
        $this->view->set('titles', json_encode($titles));
        return $this->view->render('photo/news');
    }

    /**
     * listAction 
     * 文章分类列表页
     * 
     * @return void
     */
    public function listAction() {

        $cid = intval($this->request->get('cid'));
        $page = intval($this->request->get('page', 1));

        if (!$cid) {
            return $this->errorMessage('参数错误');
        }

        $pageOptions = array('perPage' => 12,
                             'currentPage' => $page);

        try {
            $articleService =& Service::factory('Article');
            $articles = $articleService->listByCid($cid, $pageOptions);

            $cateService =& Service::factory('Cate');
            $cate = $cateService->getInfo($cid);
        } catch (Exception $e) {
            return $this->errorMessage($e->getMessage());
        }

        $this->_listCates();
        $this->view->set('cate', $cate);
        $this->view->set('page', $pageOptions);
        $this->view->set('articles', $articles);
        return $this->view->render('photo/list');
    }

    /**
     * viewAction 
     * 文章详细页
     * 
     * @return void
     */
    public function viewAction() {

        $aid = intval($this->request->get('aid'));
        if (!$aid) {
            return $this->errorMessage('参数错误');
        }

        try {
            $articleService =& Service::factory('Article');
            $article = $articleService->getInfo($aid);
        } catch (Exception $e) {

        }

        $this->view->set('article', $article);
        return $this->view->render('photo/view');
    }

    /**
     * _listCates 
     * 
     * @return void
     */
    private function _listCates() {

        try {

            $cateService =& Service::factory('Cate');
            $cates = $cateService->listByParentId(2);
        } catch (Exception $e) {
            return $this->_errorMessage($e->getMessage());
        }

        $this->view->set('cates', $cates);
    }
}
